<?php
class Cart_model extends CI_Model{
	//Add an item to the cart  
	function validate_add_cart_item(){  
	  
	    $id = $this->input->post('product_id'); // Assign posted product_id to $id  
	    $qty = $this->input->post('quantity'); // Assign posted quantity to $cty  
	  
	   // $this->db->where('id', $id); // Select where id matches the posted id
	   $this->db->select('p.id, p.sku, p.name,	p.price, p.image, p.discount, p.quantity, m.name AS manufacturer_name'); 
	   $this->db->from('products p');
	   $this->db->where('p.id',$id);
	   $this->db->order_by('p.order');
	   $this->db->join('manufacturers m', 'm.id = p.manufacturer_id');

	   $query = $this->db->get();
	   //$query = $this->db->get_where('products',array('id' => $id)); // Select the products where a match is found and limit the query by 1  
	  
	    // Check if a row has matched our product id  
	    if($query->num_rows() > 0){  
	  
	    // We have a match!  
	        foreach ($query->result() as $row)  
	        {  
	            // Create an array with product information  
	            $data = array(  
	                    'id'      => $id,  
	                    'qty'     => $qty,
	                    'price'   => $row->price,  
	                    'name'    => $row->name,
	            		'options' => array('manufacturer_name' => $row->manufacturer_name,'image' => $row->image)  
	            );  
	  
	            // Add the data to the cart using the insert function that is available because we loaded the cart library  
	            $this->cart->insert($data);   
	  
	            return TRUE; // Finally return TRUE  
	        }  
	  
	    }else{  
	        // Nothing found! Return FALSE!  
	        return FALSE;  
	    } 
	    
	} 
	
	public function validate_update_cart_item(){
		
		try {
			$rowid = $_POST['rowid'];
			$qty = $_POST['quantity'];
			//$rowid = $this->input->post('rowid'); // Assign posted rowid to $id  
	    	//$qty = $this->input->post('quantity'); // Assign posted quantity to $cty
			$data = array(
				'rowid' => $rowid,
				'qty' => $qty
			);
		
	    	$this->cart->update($data);
	    	return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}

	}
	
	public function validate_remove_cart_item(){
		
		try {
			$rowid = $_POST['rowid'];
			$qty = $_POST['quantity'];
			//$rowid = $this->input->post('rowid'); // Assign posted rowid to $id  
	    	//$qty = $this->input->post('quantity'); // Assign posted quantity to $cty
			$data = array(
				'rowid' => $rowid,
				'qty' => $qty
			);
		
	    	$this->cart->update($data);
	    	return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}
	
}